package com.sugon.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

/**
 * Shiro权限标签(Velocity版)
 *
 * @author lipengjun
 * @date 2017年11月19日 上午9:49:19
 */
public class VelocityShiro {

    /**
     * 是否拥有该权限
     *
     * @param permission 权限标识
     * @return true：是     false：否
     */
    public boolean hasPermission(String permission) {
        Subject subject = SecurityUtils.getSubject();
        return subject != null && subject.isPermitted(permission);
    }

    /**
     * 是否拥有该角色
     *
     * @param roles 角色标识
     * @return true：是     false：否
     */
    public boolean hasAnyRole(String... roles) {
        Subject subject = SecurityUtils.getSubject();
        if (subject == null) {
            return false;
        }
        PrincipalCollection principals = subject.getPrincipals();
        for (String role : roles) {
            if (subject.hasRole(role)) {
                return true;
            }
        }
        return false;
    }

}
